﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Policy;

namespace BrokerBase.Lib.Product.RateFactorConverter
{
    public class HoldAgeConverter : AbstractRateFactorConverter
    {
        public static string[] GetKeys(IPolicy policy, int age, RateFactorAxis axis)
        {
            string[] result = new string[2];

            IList<IRateFactor> rfs = null;

            if (axis == RateFactorAxis.Row)
            {
                rfs = policy.Product.RowRateFactors;

                if (rfs[1].IsEmpty)
                {
                    result[0] = age.ToString();
                }
                else
                {
                    string[] keys1 = rfs[0].Items.Split(';').Where(p => !string.IsNullOrEmpty(p)).ToArray<string>();
                    string[] keys2 = rfs[1].Items.Split(';').Where(p => !string.IsNullOrEmpty(p)).ToArray<string>();

                    int count = keys1.Count();

                    for (int i = 0; i < count; i++)
                    {
                        if (age >= int.Parse(keys1[i]) && age < int.Parse(keys2[i]))
                        {
                            result[0] = keys1[i];
                            result[1] = keys2[i];
                        }
                    }
                }
            }
            else if (axis == RateFactorAxis.Col)
            {
                //暂时只处理单个年龄，不处理年龄区间。
                result[0] = age.ToString();

            }
            return result;
        }

        private readonly string[] alias = new string[] { "投保人年龄", "投保人年龄1", "投保人年龄2" };

        public override string[] Alias
        {
            get { return alias; }
        }

        public override bool IsToPropertiesEnable
        {
            get { return false; }
        }

        public override void ToProperties()
        {
            throw new NotImplementedException();
        }

        public override bool IsPolicySelectedEnable
        {
            get { return false; }
        }

        public override void ToPolicySelected(Policy.IPolicy policy, string item)
        {
            throw new NotImplementedException();
        }

        public override string GetItemByPolicy(Policy.IPolicy policy)
        {
            int age = policy.Owner.HolderAge;

            policy.SetSettingLock(Owner.Name, true);

            int index = 0;

            string[] r = GetKeys(policy, age, Owner.Axis);

            return index == 0 ? r[0] : r[1];
        }
    }
}
